export const RandomCode = {
  code: (strlenght: number) => {
    const alphabet = 'qwert1yuiopa8sdfghjkl5zxcv2bn7mQWERT6YUIO4PASDF0GH3JKLZXCVBNM9';
    // 生成的字符串固定长度
    let left = '';
    for (var i = 0; i < strlenght; i++) {
      //    right = right + (min + (Random().nextInt(max - min))).toString();
      left = left + alphabet[Math.floor(Math.random() * alphabet.length)];
    }
    return left;
  },
};

export const urlsafe_b64encode = (base64Str: string) => {
  return base64Str
    .replace(/\+/g, '-') // Convert '+' to '-'
    .replace(/\//g, '_'); // Convert '/' to '_'
  // .replace(/=+$/, '');
};

export function isWeChat() {
  if (navigator.userAgent.toLowerCase().includes('micromessenger')) {
    return true;
  }
  if (
    navigator.userAgent.toLowerCase().indexOf('micromessenger') > -1 ||
    typeof (navigator as any).wxuserAgent !== 'undefined'
  ) {
    return true;
  }

  // 使用微信 JS 对象判断是否微信内置浏览器
  // 建议使用此方法进行判断
  if (typeof (window as any).WeixinJSBridge !== 'undefined') {
    return true;
  }
}

export function judgeBrand(_sUserAgent: string) {
  const sUserAgent = _sUserAgent.toLowerCase();
  const isIphone = sUserAgent.match(/iphone/i)?.[0] === 'iphone';
  const isHuawei = sUserAgent.match(/huawei/i)?.[0] === 'huawei';
  const isHonor = sUserAgent.match(/honor/i)?.[0] === 'honor';
  const isOppo = sUserAgent.match(/oppo/i)?.[0] === 'oppo';
  const isOppoR15 = sUserAgent.match(/pacm00/i)?.[0] === 'pacm00';
  const isVivo = sUserAgent.match(/vivo/i)?.[0] === 'vivo';
  const isXiaomi = sUserAgent.match(/mi\s/i)?.[0] === 'mi ';
  const isXiaomi2s = sUserAgent.match(/mix\s/i)?.[0] === 'mix ';
  const isRedmi = sUserAgent.match(/redmi/i)?.[0] === 'redmi';
  const isSamsung = sUserAgent.match(/sm-/i)?.[0] === 'sm-';

  if (isIphone) {
    return 'iphone';
  } else if (isHuawei || isHonor) {
    return 'huawei';
  } else if (isOppo || isOppoR15) {
    return 'oppo';
  } else if (isVivo) {
    return 'vivo';
  } else if (isXiaomi || isRedmi || isXiaomi2s) {
    return 'xiaomi';
  } else if (isSamsung) {
    return 'samsung';
  } else {
    return 'default';
  }
}

export function isWeixinBrowser() {
  return /micromessenger/.test(navigator.userAgent.toLowerCase());
}
